package com.sgb.kjwl.view.ui.adapter.material;

import android.content.Context;
import android.text.TextUtils;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.SkuCheckEntity;
import com.sgb.kjwl.model.entity.SkuCombinationEntity;
import com.sgb.kjwl.model.entity.user.SkuItemAttrEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class GoodsSkuCheckAdapter extends BaseRecycleAdapter2<SkuItemAttrEntity> {
    private RecyclerView recyclerView;
    private SkuChildAdapter skuChildAdapter;
    public List<SkuCheckEntity> check = null;
    private List<SkuCombinationEntity> list = null;
    private IPrice iPrice;
    private boolean isFirst = true;
    private int sum = 0;
    public boolean isClic;

    public GoodsSkuCheckAdapter(Context context, List<SkuItemAttrEntity> data) {
        super(context, R.layout.item_check_sku, data);
    }


    public void setCheckData(List<SkuCheckEntity> check, List<SkuCombinationEntity> list) {
        this.check = check;
        this.list = list;
        notifyDataSetChanged();
    }

    public String getPrice() {
        String price = "0.00";
        for (int a = 0; a < list.size(); a++) {
            List<SkuCombinationEntity.CombinationList> combinationLists = list.get(a).getList();
            boolean test = checkSame(combinationLists);
            if (test) {
                return list.get(a).getMoney();
            }
        }
        return price;
    }

    public int getCheckPosition() {
        for (int a = 0; a < list.size(); a++) {
            List<SkuCombinationEntity.CombinationList> combinationLists = list.get(a).getList();
            boolean test = checkSame(combinationLists);
            if (test) {
                return a;
            }
        }
        return 0;
    }

    public int getStatus() {
        int status = 0;
        for (int a = 0; a < list.size(); a++) {
            List<SkuCombinationEntity.CombinationList> combinationLists = list.get(a).getList();
            boolean test = checkSame(combinationLists);
            if (test) {
                return list.get(a).getPromoteStatus();
            }
        }
        return status;
    }

    public String getId() {
        String strId = "";
        for (int a = 0; a < list.size(); a++) {
            List<SkuCombinationEntity.CombinationList> combinationLists = list.get(a).getList();
            boolean test = checkSame(combinationLists);
            if (test) {
                return list.get(a).getId();
            }
        }
        return strId;
    }

    public String getPromotePrice() {
        String price = "0.00";
        for (int a = 0; a < list.size(); a++) {
            List<SkuCombinationEntity.CombinationList> combinationLists = list.get(a).getList();
            boolean test = checkSame(combinationLists);
            if (test) {
                if (list.get(a).getPromoteStatus() == 2 || list.get(a).getPromoteStatus() == 1)
                    return list.get(a).getPromotePrice();
            }
        }
        return price;
    }

    public boolean checkSame(List<SkuCombinationEntity.CombinationList> combinationLists) {
        for (int b = 0; b < check.size(); b++) {
            boolean isSame = checkLast(check.get(b).getVal(), combinationLists);
            if (!isSame) {
                return false;
            }
        }
        return true;
    }

    public boolean checkLast(String val, List<SkuCombinationEntity.CombinationList> combinationLists) {
        for (int c = 0; c < combinationLists.size(); c++) {
            if (!TextUtils.isEmpty(combinationLists.get(c).getVal()) && combinationLists.get(c).getVal().equals(val)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void convert(ItemViewHolder holder, SkuItemAttrEntity entity, int position) {
        holder.setBinding(BR.viewModel, entity);
        recyclerView = holder.itemView.findViewById(R.id.params_list);
        skuChildAdapter = new SkuChildAdapter(context, entity.getList());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(skuChildAdapter);
        skuChildAdapter.setItemSelect(true);
        if (isFirst) {
            iPrice.getMoney1(list.get(getCheckPosition()),1,getCheckPosition());
            isFirst = false;
        }
        skuChildAdapter.setOnItemClick(new AdapterOnItemClick<SkuItemAttrEntity.Child>() {

            @Override
            public void onItemClick(SkuItemAttrEntity.Child child, int pos) {
                datas.get(position).getList().get(pos).setChecked1(true);
                for (int x = 0; x < check.size(); x++) {
                    String checkId = check.get(x).getId() + "";
                    if (entity.getId().equals(checkId)) {
                        check.get(x).setVal(child.getValData());
                        check.get(x).setValId(child.getValId());
                        check.get(x).setItemParamId(child.getItemParamId());
                        break;
                    }
                }
                iPrice.getMoney1(list.get(getCheckPosition()),0,getCheckPosition());
            }
        });
    }

    public interface IPrice {
        void getMoney(String money);

        void getMoney1(SkuCombinationEntity skuCombinationEntity,int type,int position);
    }

    public void OnPrice(IPrice iPrice) {
        this.iPrice = iPrice;
    }
}
